<?php

class multiple_curl_threads {
    
    const ERROR         = 'error';
    const ERROR_NO      = 'error_no';
    const ERROR_STRING  = 'error_string';
    const CONTENT       = 'content';

    protected $_master_hanlder;
    protected $_child_handlers;
    protected $_responses;
    public $_post_fields;
    
    public function __construct() {
        $this->_master_hanlder = curl_multi_init();
        $this->_responses = array();
        $this->_child_handlers = array();
        $this->_post_fields = array();
    }

    public function add_handler($id, $url, array $curl_options = array()) {
        $this->_responses[$id] = array();
        $this->_child_handlers[$id] = curl_init($url);
        curl_setopt($this->_child_handlers[$id], CURLOPT_HEADER, 0);
        curl_setopt($this->_child_handlers[$id], CURLOPT_RETURNTRANSFER, true);
        curl_setopt($this->_child_handlers[$id], CURLOPT_TIMEOUT, 30);

        if (count($curl_options) > 0) {
            foreach ($curl_options as $option => $value) {
                if (!curl_setopt($this->_child_handlers[$id], $option, $value)) {
                    show_error("Invalid curl options for $id");
                }
            }
        }

        if(count($this->_post_fields[$id])>0){
            curl_setopt($this->_child_handlers[$id], CURLOPT_POST,1);
            curl_setopt($this->_child_handlers[$id], CURLOPT_POSTFIELDS,http_build_query($this->_post_fields[$id]));
        }

        curl_multi_add_handle($this->_master_hanlder, $this->_child_handlers[$id]);
    }

    public function start($auto_close=TRUE) {
        $running = NULL;
        do {
            curl_multi_exec($this->_master_hanlder, $running);
            usleep(25000);
        } while ($running > 0);
        //@TO_DO : rasponse should be semt as an object not array
        foreach ($this->_child_handlers as $id => $value) {            
            if (curl_errno($value) === 0) {
                $this->_responses[$id][self::ERROR] = FALSE;
                $this->_responses[$id][self::CONTENT] = curl_multi_getcontent($value);
            } else {
                $this->_responses[$id][self::ERROR] = TRUE;
                $this->_responses[$id][self::ERROR_NO] = curl_errno($value);
                $this->_responses[$id][self::ERROR_STRING] = curl_error($value);
            }
            
            if($auto_close){
                curl_multi_remove_handle($this->_master_hanlder, $value);
            }
        }

        if($auto_close){
            curl_multi_close($this->_master_hanlder);
        }
        
        
    }

    public function get_responses() {
        return $this->_responses;
    }

    public function get_response($id){
        return $this->_responses[$id];
    }

    public function close(){
       foreach ($this->_child_handlers as $id => $value) {
          curl_multi_remove_handle($this->_master_hanlder, $value);
       }
       curl_multi_close($this->_master_hanlder);
    }

    public function debub_print($handler_id=NULL){
        if($handler_id){
            debug_dump(curl_getinfo($this->_child_handlers[$handler_id]));
        }else{
             foreach ($this->_child_handlers as $id => $value) {
                 debug_dump(curl_getinfo($this->_child_handlers[$id]));
             }
            
        }
    }

    public function add_post_data($id,$key,$value){        
        $this->_post_fields[$id][$key] = $value;
    }


}

?>